package com.hb.trade.api.manager.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.dto.RoleDTO;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.center.au.entity.Role;
import com.hb.trade.api.manager.entity.AdminLoginUser;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.center.RoleClient;
import com.hb.trade.client.center.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.DisabledAccountException;
import org.fzzn.core.auth.shiro.ex.BlackListException;
import org.fzzn.core.auth.shiro.service.DefaultUserEntityService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.user.login.SimpleUserExample;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Shiro登录查询用户的实现类
 *
 * @author zhaojk
 */
@Slf4j
@Service
public class AdminUserEntityService extends DefaultUserEntityService {

    protected final UserClient userClient;
    protected final RoleClient roleClient;
    protected final OrgClient orgClient;

    public AdminUserEntityService(UserClient userClient,
                                  RoleClient roleClient,
                                  OrgClient orgClient) {
        this.userClient = userClient;
        this.roleClient = roleClient;
        this.orgClient = orgClient;
    }

    /**
     * 该方法为登录时shiroRealm调用
     * 该方法的异常最终将在Controller层被捕获
     * 有异常直接抛出即可
     *
     * @param username
     * @return
     */
    @Override
    public SimpleUserExample getByUsername(String username) {
        UserDTO user;
        Message<UserDTO> message = userClient.getByUsername(username);
        if (message.isFail()) {
            log.warn(message.getMessage());
            return null;
        }
        user = message.getData();

        /* 判断用户状态等操作, 用户被禁用, 用户列入黑名单等操作 start */
        // 是否被禁用
        Integer userStatus = user.getUserStatus();
        boolean disabledFlag = Objects.equals(userStatus, 2);
        if (disabledFlag) {
            throw new DisabledAccountException();
        }

        // 是否被列入黑名单
        Message<Boolean> exiMsg = userClient.existsInBlackList(user);
        if (exiMsg.isSuccess() && exiMsg.getData()) {
            throw new BlackListException();
        }
        /* 判断用户状态等操作, 用户被禁用, 用户列入黑名单等操作 end */

        // 查询用户的企业信息
        Message<OrgDTO> org = orgClient.getOrgByUsername(username);
        OrgDTO data = org.getData();

        return new AdminLoginUser(user, JSONUtil.parseObj(data));
    }

    /**
     * 查询用户所拥有的角色
     *
     * @param username
     * @return
     */
    @Override
    public List<String> listRolePermissions(String username) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(username);
        Message<List<RoleDTO>> roleDTOSMsg = roleClient.listRolesByUser(userDTO);
        List<RoleDTO> roleDTOS = roleDTOSMsg.getData();
        List<String> map = CollUtil.map(roleDTOS, Role::getRoleCode, true);

        log.info("判断当前用户的角色权限: {}", map);
        return map;
    }

    @Override
    public List<String> listFuncPermissions(String username) {
        return null;
    }


}
